package com.zhc.dejingapi.utils.qing;

import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.zhc.dejingapi.entity.query.FilterQuery;
import com.zhc.dejingapi.entity.vo.DateAndNumVo;
import com.zhc.dejingapi.pojo.Report.*;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.utils.HttpClientUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhc
 * @date 2024年08月22日 16:13
 */
@Component
public class QingflowChartDataGetUtilsTwo {

    public List<Object> GetChartTableData1(QingflowApiInfo qqingflowApiInfo, String strChartKey, FilterQuery body) throws Exception {

        List<Object>  list= new ArrayList<>();
        String strUrl = qqingflowApiInfo.getServerUrl();
        if (strUrl.equals("/")) {
            strUrl += "/";
        }
        strUrl += qqingflowApiInfo.getBaseUrl();
        if (strUrl.equals("/")) {
            strUrl += "/";
        }

        //http://39.185.158.205:1080/openApi/chart/80ri9qo81o02/apply/filter
        strUrl += "chart/" + strChartKey + "/apply/filter";
        String strBody="";
        if(body==null){
            strBody = "{\n" +
                    "    \"accurateQuery\": [\n" +
                    "        {\n" +
                    "            \"queId\": \"\",\n" +
                    "            \"searchKey\": \"\"\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}";
        }else{
            cn.hutool.json.JSONObject json = JSONUtil.parseObj(body, false,true);
            strBody =json.toString();

        }

        Map<String, String> mapHeadPara = new HashMap<>();
        mapHeadPara.put("accessToken", accessTokenUtils.getAccessToken(qqingflowApiInfo));

        String strBack = HttpClientUtil.doPost(strUrl, strBody, mapHeadPara);
        if (strBack == null || strBack.isEmpty()) {
            throw new Exception("出错：返回内容为空！");
        }
        Gson gson = new Gson();
        QingFlowTableReportResultMainModel tableReportResultMainModel = gson.fromJson(strBack, QingFlowTableReportResultMainModel.class);

        if (tableReportResultMainModel == null) {
            throw new Exception("出错：返回内容为空！");
        }

        if (tableReportResultMainModel.getErrCode() != 0) {
            throw new Exception("出错：返回内容错误！" + tableReportResultMainModel.getErrMsg());
        }
        if (tableReportResultMainModel.getResult() == null || tableReportResultMainModel.getResult().getResult() == null) {
            throw new Exception("出错：返回内容为空！");
        }

        List<QingFlowTableReportResultRowItem> listRows = tableReportResultMainModel.getResult().getResult();

        for (QingFlowTableReportResultRowItem rowItem : listRows) {
            HashMap<String,Object> map = new HashMap<>();

            if (rowItem == null || rowItem.getAnswers() == null || rowItem.getAnswers().isEmpty()) {

            } else {
                for (QingFlowTableReportResultFieldItem fieldItem : rowItem.getAnswers()) {
                    if (fieldItem == null) {

                    } else {

                        if (fieldItem.getValues() == null || fieldItem.getValues().isEmpty()) {
                            map.put(fieldItem.getQueTitle(), (Object) null);
                        } else {
                            map.put(fieldItem.getQueTitle(), fieldItem.getValues().get(0).getValue());
                        }
                    }
                }

                list.add(map);
            }
        }

        return list;
    }

    public List<DateAndNumVo> GetChartPieDataOne(QingflowApiInfo qqingflowApiInfo,String strChartKey) throws Exception {

        List<DateAndNumVo>  list= new ArrayList<>();
        String strUrl = qqingflowApiInfo.getServerUrl();
        if (strUrl.equals("/")) {
            strUrl += "/";
        }
        strUrl += qqingflowApiInfo.getBaseUrl();
        if (strUrl.equals("/")) {
            strUrl += "/";
        }

        //http://39.185.158.205:1080/openApi/chart/80ri9qo81o02/apply/filter
        strUrl += "chart/" + strChartKey + "/chartData";

        String strBody = "{\n" +
                "    \"accurateQuery\": [\n" +
                "        {\n" +
                "            \"queId\": \"\",\n" +
                "            \"searchKey\": \"\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        Map<String, String> mapHeadPara = new HashMap<>();
        mapHeadPara.put("accessToken", accessTokenUtils.getAccessToken(qqingflowApiInfo));

        String strBack = HttpClientUtil.doPost(strUrl, strBody, mapHeadPara);
        if (strBack == null || strBack.isEmpty()) {
            throw new Exception("出错：返回内容为空！");
        }
        Gson gson = new Gson();
        QingFlowChartReportResultMainModel chartResultMainModel = gson.fromJson(strBack, QingFlowChartReportResultMainModel.class);

        if (chartResultMainModel == null) {
            throw new Exception("出错：返回内容为空！");
        }

        if (chartResultMainModel.getErrCode() != 0) {
            throw new Exception("出错：返回内容错误！" + chartResultMainModel.getErrMsg());
        }

        if (chartResultMainModel.getResult().getChartSeries() == null || chartResultMainModel.getResult().getChartSeries().isEmpty() ){
            throw new Exception("出错：返回内容错误！" + chartResultMainModel.getErrMsg());
        }
        else{
            List<QingFlowChartReportResultChartSerieItem> listItems = chartResultMainModel.getResult().getChartSeries();
            for (QingFlowChartReportResultChartSerieItem SerieItem : listItems){

                if(SerieItem.getSeriesValues()==null||SerieItem.getSeriesValues().isEmpty()){

                }
                else{

                    for (QingFlowChartReportResultChartSeriesValue seriesValue : SerieItem.getSeriesValues()){
                        DateAndNumVo dateAndNumVo = new DateAndNumVo();
                        dateAndNumVo.setDate(seriesValue.getRowName());
                        dateAndNumVo.setNum(seriesValue.getValue());
                        list.add(dateAndNumVo);
                    }

                }

            }

        }

        return list;
    }

}
